import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';

enum FilterType {
  OneColumn,
  Calendar,
}

class MenuItemBean {
  String title;
  String showType;
  String filterKey;
  FilterType filterType;

  basicFromJson(Map<String, dynamic> json) {
    this.title = json['title'];
    this.showType = json['showType'];
    this.filterKey = json['filterKey'];
    switch (showType) {
      case "oneColumn":
        filterType = FilterType.OneColumn;
        break;
      case "calendar":
        filterType = FilterType.Calendar;
        break;
    }
  }
}

class OneColumnSubItemBean {
  String title;
  String code;
  bool isSelected;
  OneColumnSubItemBean({this.title,this.code,this.isSelected=false});

  OneColumnSubItemBean.fromJson(Map<String, dynamic> json) {
    this.title = json['title'];
    this.code = json['code'];
    this.isSelected = false;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['code'] = this.code;
    return data;
  }
}

//单列数据
class OneColumnBean with MenuItemBean {
  List<OneColumnSubItemBean> itemArray;

  FilterType filterType = FilterType.OneColumn;

  OneColumnBean.fromJson(Map<String, dynamic> json) {
    this.title = json['title'];
    this.showType = json['showType'];
    this.filterKey = json['filterKey'];
    this.itemArray = (json['itemArray'] as List) != null
        ? (json['itemArray'] as List)
        .map((i) => OneColumnSubItemBean.fromJson(i))
        .toList()
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['showType'] = this.showType;
    data['filterKey'] = this.filterKey;

    data['itemArray'] = this.itemArray != null
        ? this.itemArray.map((i) => i.toJson()).toList()
        : null;
    return data;
  }
}

//日历数据
class CalendarBean with MenuItemBean {
  String selectType;

  String startTime;

  String endTime;

  FilterType filterType = FilterType.Calendar;

  CalendarBean({
    this.selectType,
    this.startTime,
    this.endTime,
  });

  CalendarBean.fromJson(Map<String, dynamic> srcJson) {
    basicFromJson(srcJson);
    //this.selectType
    Map filterAction = srcJson["filterAction"];
    selectType = filterAction["selectType"];
    startTime = filterAction["startTime"];
    endTime = filterAction["endTime"];
  }
}

//配置数据
class ListConfigData {
  ListDefaultRequestDataBean listDefaultRequestData;

  List<RightNavigationItem> rightNavigationDataList = [];

  List<MenuItemBean> filterData;

  SearchData searchData;

  List<OneColumnSubItemBean> selectSubItemBeans;

  ListConfigData.fromJson(Map<String, dynamic> srcJson) {
    this.listDefaultRequestData = srcJson['listDefaultRequestData'] != null ? ListDefaultRequestDataBean.fromJson(srcJson['listDefaultRequestData']) : null;
    rightNavigationDataList = (srcJson["rightNavigationData"] as List != null)
        ? (srcJson["rightNavigationData"] as List)
        .map((i) => RightNavigationItem.fromJson(i))
        .toList()
        : [];
    this.searchData = srcJson['searchData'] != null ? SearchData.fromJson(srcJson['searchData']) : null;
    this.filterData = (srcJson["filterData"] as List != null)
        ? (srcJson["filterData"] as List)
        .map((i) => FilterArrayFactory.createFilterBean(i))
        .toList()
        : [];

    selectSubItemBeans = List< OneColumnSubItemBean>();
    for(int i = 0; i< filterData.length; i++) {
      MenuItemBean itemBean = filterData[i];
      if(itemBean.filterType == FilterType.OneColumn && (itemBean as OneColumnBean)!= null) {
        (itemBean as OneColumnBean).itemArray[0].isSelected = true;
        selectSubItemBeans.add((itemBean as OneColumnBean).itemArray[0]);
      } else {
        selectSubItemBeans.add(OneColumnSubItemBean());
      }
    }
  }
}

//导航item数据
class RightNavigationItem extends Object {
  String showType;

  NavigationItemTextStyle textStyle;

  String route;

  ImageStyle imageStyle;

  RightNavigationItem({
    this.showType,
    this.textStyle,
    this.route,
  });

  RightNavigationItem.fromJson(Map<String, dynamic> srcJson) {
    showType = srcJson["showType"];
    route = srcJson["route"];
    textStyle = (srcJson["textStyle"] != null)
        ? NavigationItemTextStyle.fromJson(srcJson["textStyle"])
        : null;
    imageStyle = (srcJson["imageStyle"] != null)
        ? ImageStyle.fromJson(srcJson["imageStyle"])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["showType"] = showType;
    data["textStyle"] = textStyle.toJson();
    if(this.textStyle != null) {
      data["textStyle"] =  textStyle.toJson();
    }
    if (this.imageStyle != null) {
      data['imageStyle'] = this.imageStyle.toJson();
    }
    data["route"] = route;
    return data;
  }
}

//文字样式
class NavigationItemTextStyle {
  String text;

  String textColor;

  NavigationItemTextStyle({
    this.text,
    this.textColor,
  });

  NavigationItemTextStyle.fromJson(Map<String, dynamic> srcJson) {
    text = srcJson["text"];
    textColor = srcJson["textColor"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["text"] = text;
    data["textColor"] = textColor;
    return data;
  }
}

//导航图片model
class ImageStyle {
  //图片地址
  String image;

  ImageStyle.fromJson(Map<String, dynamic> srcJson) {
    image = srcJson["image"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["image"] = image;
    return data;
  }
}

//创建筛选model数组
class FilterArrayFactory {
  static MenuItemBean createFilterBean(Map<String, dynamic> json) {
    if ((json["showType"] is String) != true) {
      return null;
    }
    if (json["showType"] == "oneColumn") {
      return OneColumnBean.fromJson(json);
    }
    if (json["showType"] == "calendar") {
      return CalendarBean.fromJson(json);
    }
    return null;
  }

  List<MenuItemBean> createFilterList(List filterList) {
    if ((filterList is List) != true) {
      return null;
    }
    List list = [];
    for (int i = 0; i < filterList.length; i++) {
      Map item = filterList[i];
      MenuItemBean bean = createFilterBean(item);
      list.add(bean);
    }
    return list;
  }
}

//精确搜搜的数据模型
class SearchItem {
  String title;
  String jumpType;
  String route;
  String searchKey;

  SearchItem({
    this.title,
    this.jumpType,
    this.route,
    this.searchKey,
  });

  SearchItem.fromJson(Map<String, dynamic> srcJson) {
    title = srcJson["title"];
    jumpType = srcJson["jumpType"];
    searchKey = srcJson["searchKey"];
    route = srcJson["route"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["title"] = title;
    data["jumpType"] = jumpType;
    data["searchKey"] = searchKey;
    data["route"] = route;
    return data;
  }
}

//搜索数据
class SearchData {
  String cacheKey;
  String placeholder;
  String type;
  List<SearchItem> listData;

  //模糊搜索的key
  String dimKey;

  SearchData({
    this.cacheKey,
    this.placeholder,
    this.type,
    this.listData,
  });

  SearchData.fromJson(Map<String, dynamic> json) {
    this.cacheKey = json["cacheKey"];
    this.placeholder = json['placeholder'];
    this.type = json['type'];
    if(stringNotEmpty(json['dimKey'])) {
      this.dimKey = json['dimKey'];
    }
    this.listData = (json['listData'] as List) != null
        ? (json['listData'] as List).map((i) => SearchItem.fromJson(i)).toList()
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["cacheKey"] = cacheKey;
    data['placeholder'] = this.placeholder;
    data['type'] = this.type;
    data['dimKey'] = this.dimKey;
    data['listData'] = this.listData != null
        ? this.listData.map((i) => i.toJson()).toList()
        : null;
    return data;
  }
}

class SearchCallBackBean {
  String searchKey;
  String searchValue;
  String displayName;

  SearchCallBackBean({this.searchKey, this.searchValue, this.displayName});

  SearchCallBackBean.fromJson(Map<String, dynamic> json) {
    this.searchKey = json['searchKey'];
    this.searchValue = json['searchValue'];
    this.displayName = json['displayName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['searchKey'] = this.searchKey;
    data['searchValue'] = this.searchValue;
    data['displayName'] = this.displayName;
    return data;
  }

}

//列表默认请求数据
class ListDefaultRequestDataBean {
  SearchDefaultRequestBean searchDefaultRequest;

  ListDefaultRequestDataBean({this.searchDefaultRequest});

  ListDefaultRequestDataBean.fromJson(Map<String, dynamic> json) {
    this.searchDefaultRequest = json['searchDefaultRequest'] != null ? SearchDefaultRequestBean.fromJson(json['searchDefaultRequest']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.searchDefaultRequest != null) {
      data['searchDefaultRequest'] = this.searchDefaultRequest.toJson();
    }
    return data;
  }
}

//默认搜索数据
class SearchDefaultRequestBean {
  String searchKey;
  String searchValue;
  String displayName;

  SearchDefaultRequestBean({this.searchKey, this.searchValue, this.displayName});

  SearchDefaultRequestBean.fromJson(Map<String, dynamic> json) {
    this.searchKey = json['searchKey'];
    this.searchValue = json['searchValue'];
    this.displayName = json['displayName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['searchKey'] = this.searchKey;
    data['searchValue'] = this.searchValue;
    data['displayName'] = this.displayName;
    return data;
  }
}